package com.treasure.service.loan.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.treasure.service.commons.lang.util.BeanUtil;
import com.treasure.service.commons.lang.util.IdCreater;
import com.treasure.service.constants.LoanChannel;
import com.treasure.service.constants.OrderStatus;
import com.treasure.service.domain.TLoan;
import com.treasure.service.domain.TLoanExample;
import com.treasure.service.domain.TLoanOrder;
import com.treasure.service.domain.TUserInfo;
import com.treasure.service.dto.LoanDto;
import com.treasure.service.dto.TLoanOrderDto;
import com.treasure.service.mapper.TLoanMapper;
import com.treasure.service.mapper.TLoanOrderMapper;
import com.treasure.service.mapper.TUserInfoMapper;
import com.treasure.service.third.utils.HttpUtil;
import com.treasure.service.result.PageResult;

@Service("loanService")
public class LoanServiceImpl implements LoanService {
	@Autowired
	private TLoanMapper tLoanMapper;
    @Autowired
    private TUserInfoMapper tUserInfoMapper;
	@Autowired
	private TLoanOrderMapper tLoanOrderMapper;
	@Override
	public PageResult<List<LoanDto>> getLoanList(int pageNum,int pageSize) {
		Page page = PageHelper.startPage(pageNum,pageSize);
		TLoanExample tLoanExample = new TLoanExample();
		tLoanExample.createCriteria().andStatusEqualTo(1);
		tLoanExample.setOrderByClause("sort_num");
		List<TLoan> list = tLoanMapper.selectByExample(tLoanExample);
		List<LoanDto> result=new ArrayList<>();
		list.forEach(tloan -> {
			LoanDto loanDto = BeanUtil.copy(tloan,LoanDto::new);
			result.add(loanDto);
		});

		return new PageResult<>(page,page);
	}
	
	/* 
	 * 创建贷款订单
	 */
	@Override
	public String createLoanOrder(TLoanOrderDto tLoanOrderDto) {
    	String url = "https://91k.91kab.com/oauth/apply3";
    	Map<String,String> params = new HashMap<String,String>();
    	params.put("name", tLoanOrderDto.getRealName());
    	params.put("token", "4fff6d7383574d08b0470f37e5baa72c");
    	params.put("idcard", tLoanOrderDto.getCertNo());
    	params.put("tel", tLoanOrderDto.getMobile());
    	params.put("loanid", tLoanOrderDto.getLoanId());
    	String result = HttpUtil.doPost(url, params);
    	JSONObject jsonObject = JSONObject.parseObject(result);
    	String applyUrl = jsonObject.getString("data");
    	
    	
    	//创建本地订单
    	TLoanOrder order = new TLoanOrder();
    	order.setId(IdCreater.getLongId());
    	order.setCustomerId(tLoanOrderDto.getCustomerId());
		TUserInfo tUserInfo = tUserInfoMapper.selectByPrimaryKey(tLoanOrderDto.getCustomerId());
		if (tUserInfo.getInviteCode() != null){
			tUserInfo = new TUserInfo();
			order.setInviterId(tUserInfo.getInviteCode());
		}
    	order.setRealName(tLoanOrderDto.getRealName());
    	order.setCertNo(tLoanOrderDto.getCertNo());
    	order.setMobile(tLoanOrderDto.getMobile());
    	order.setLoanId(tLoanOrderDto.getLoanId());
    	order.setApplyUrl(applyUrl);
    	order.setStatus(OrderStatus.LoanOrder.HAVESEND);
    	order.setRebateStatus(1);
    	order.setChannel(LoanChannel.JiuYi);
    	tLoanOrderMapper.insertSelective(order);
		return applyUrl;
	}

}
